package com.chen.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.Result;
import com.chen.entity.Poster;
import com.chen.mapper.PosterMapper;
import com.chen.service.impl.PosterServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author chenchao
 * @date 2022/6/17 13:42
 */
@RestController
@Api(value = "PosterController", tags = "海报封面")
@RequestMapping("/poster")
public class PosterController {

    @Value("${files.upload.path}")
    private String path;

    @Value("${server.ip}")
    private String ip;

    @Value("${server.port}")
    private String port;

    @Resource
    private PosterMapper posterMapper;


    @Autowired
    private PosterServiceImpl posterService;

    @PostMapping("/batchUpload")
    public Result batchUpload(@RequestParam("file") MultipartFile[] multipart) {

        CopyOnWriteArrayList<Poster> copy = new CopyOnWriteArrayList();
        if (multipart != null & multipart.length > 0) {
            for (MultipartFile multipartFile : multipart) {
                String fileName = multipartFile.getOriginalFilename();
//                获取海报类型
                String type = FileUtil.extName(fileName);
                String flag = IdUtil.randomUUID() + StrUtil.DOT + type;
//                获取海报大小
                long size = multipartFile.getSize()/1024;
                File file1 = new File(path + flag);
//                String url = "http://" + ip + ":" + port + "/file/" + flag;
                String url = "http://" + ip + ":" + port + "/file/" + flag;
                File parentFile = file1.getParentFile();
                Poster poster = new Poster();
                poster.setImages(url);
                poster.setSize(size);
                poster.setType(type);
                poster.setEnable(false);
                poster.setExplains("本地上传");

                copy.add(poster);

                if (!parentFile.exists()) {
                    parentFile.mkdir();
                }

                try {
                    multipartFile.transferTo(file1);
                } catch (IOException e) {
                    e.printStackTrace();
                    return Result.success("上传失败");
                }

            }
        }
        posterService.saveBatch(copy);
        System.out.println("上传成功");

        return Result.success("上传成功");


    }



    @RequestMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result selectDict(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestParam("") String comment){

        return Result.success(posterService.selectPage(pageNum, pageSize,comment));
    }


    @PutMapping("/updatePoster")
    @ApiOperation(value = "更新")
    public Result updatePoster(@RequestBody Poster poster){

        return Result.success(posterService.updateById(poster));

    }

    @DeleteMapping("/deletePoster/{id}")
    public Result deletePoster(@PathVariable("id") int id){
        return Result.success(posterService.removeById(id));
    }


    @PostMapping("/addPoster")
    @ApiOperation(value = "添加")
    public Result addPoster(@RequestBody Poster poster){

        return Result.success(posterService.save(poster));

    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(posterService.removeByIds(ids));
    }

    @GetMapping("/selectIsEnablePoster")
    @ApiOperation(value = "查询海报")
    public Result selectIsEnablePoster(){
        return Result.success(posterService.selectIsEnablePoster());
    }

    @GetMapping("/selectCoverUrlPoster")
    @ApiOperation(value = "启用登录海报")
    public Result selectCoverUrlPoster(){
        QueryWrapper query = new QueryWrapper();
        query.eq("cover_url",1);
        Optional<Poster> max=null;
        List<Poster> list = posterMapper.selectList(query);
        for (Poster poster : list) {
          max = list.stream().max(new Comparator<Poster>() {
                @Override
                public int compare(Poster o1, Poster o2) {
                    return o1.getSort() - o2.getSort();
                }
            });

        }
        return Result.success(max.get());
    }

}
